/*
 * @(#)DispatchChannel.java
 *
 * Copyright (c) 1998-2005 Jang-Ho Hwang, 
 * All Rights Reserved.
 *
 * $Id: DispatchChannel.java,v 1.1.1.1 2005/05/22 01:02:27 rath Exp $
 */
package rath.nateon;

import java.io.*;
import java.net.Socket;
/**
 * 네이트온 메신져 클래스
 * 
 * @author Jang-Ho Hwang, rath@xrath.com
 * @version 1.0.000, 2005/05/20
 */
public class DispatchChannel extends NateOnChannel
{
	private final NateOnMessenger nateon;

	public DispatchChannel( NateOnMessenger nateon, String host )
	{
		super( host );

		this.nateon = nateon;
	}

	protected void processMessage( NateOnMessage msg ) throws IOException
	{
		
	}

	protected void channelConnected() throws IOException
	{
		NateOnMessage out = new NateOnMessage("PVER");
		out.add("3.615").add("3.0");
		out.setCallback( "doAuth" );

		writeMessage( out );
	}

	public void doAuth( NateOnMessage msg ) throws IOException
	{
		NateOnMessage out = new NateOnMessage("AUTH");
		out.add("DES");
		out.setCallback( "doAuthRequest" );

		writeMessage( out );
	}

	public void doAuthRequest( NateOnMessage msg ) throws IOException
	{
		NateOnMessage out = new NateOnMessage("REQS");
		out.add("DES").add( nateon.getUserId() );
		out.setCallback( "doExchangeServer" );

		writeMessage( out );
	}

	public void doExchangeServer( NateOnMessage msg ) throws IOException
	{
		if( msg.getHeader().equals("REQS") )
		{
			isLive = false;

			String host = msg.get(1);
			int port = msg.getInt(2);

			nateon.notification = new NotificationChannel(nateon, host, port);
			nateon.notification.start();
		}
	}

	protected void channelClosed()
	{

	}

	protected void connectFailed( Exception e )
	{
		nateon.fireConnectFailed(e);
	}
}
